<?php
namespace Common\Controller;
use Think\Controller;

class BaseController extends Controller
{

    public $key = 'yuanyi';
    /**
     * 生成随机汉字
     * @param $num
     * @return string
     */
    public function getChar($num)  // $num为生成汉字的数量
    {
        $b = '';
        for ($i = 0; $i < $num; $i++) {
            // 使用chr()函数拼接双字节汉字，前一个chr()为高位字节，后一个为低位字节
            $a = chr(mt_rand(0xB0, 0xD0)) . chr(mt_rand(0xA1, 0xF0));
            // 转码
            $b .= iconv('GB2312', 'UTF-8', $a);
        }
        return $b;
    }

    /**
     * 随机二维数组
     * @param $list
     * @return array
     */
    public function shuffle_assoc($list) {
        if (!is_array($list)) return $list;

        $keys = array_keys($list);
        shuffle($keys);
        $random = array();
        foreach ($keys as $key)
            $random[$key] = $this->shuffle_assoc($list[$key]);
        return $random;
    }

    //打乱中文字符串
    public  function str_rev($str){
        //先判断参数是否为字符串，且为UTF8编码
        if(!is_string($str)||!mb_check_encoding($str,"utf-8")){
            die("输入的不是utf8类型的字符串");
        }

        //用mb_strlen函获取算utf8字符串的长度
        $length=mb_strlen($str,"utf-8");

        //声明一个数组备用
        $arr=array();

        //将字符串拆开放入数组
        for($i=0;$i<$length;$i++){
            $arr[$i]=mb_substr($str,$i,1,"utf-8");
        }
        //打乱数组
        shuffle($arr);
        //将数组中单个字符元素重新组合成字符串
        $str=implode("",$arr);

        return $str;
    }

    /**
     * post请求
     * @param $url
     * @param $postdata
     * @return mixed
     */
    public function getcomponent($url,$postdata){

        $ch = curl_init(); //用curl发送数据给api

        curl_setopt( $ch, CURLOPT_POST, true );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
        curl_setopt( $ch, CURLOPT_URL, $url );
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $postdata );
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
        curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, FALSE );
        $response = curl_exec($ch);
        curl_close( $ch );
        $rest = json_decode($response,true);
        return $rest;
    }

    /**
     * get请求
     * @param $url
     * @return mixed
     */
    public function curlget($url){
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
        curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, FALSE );
        $response = curl_exec($ch);
        curl_close( $ch );
        $rest = json_decode($response,true);
        return $rest;
    }

    /*
     * 返回数据 统一json
     * $data array 返回的数据
     * $msg string
     * $code int 返回码 200成功 100失败
     *       400 sesssion过期
     *       600需要微信认证 700需要手机绑定 或者手机重新登录
     *       401 参数错误
     * return json {'data':[]/string/{},code:200} 200成功
     * */
    public function returnData($data=array(),$msg='成功',$code=200){
        $arr=array(
            'data'=>$data,
            'msg'=>$msg,
            'code'=>$code
        );
        $str =json_encode($arr);
        echo $str;
        exit;
    }

    //获取当前域名
    public function getDomain(){
        $domain ="http://";
        if ( ! empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off')
        {
            $domain ="https://";
        }
        elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https')
        {
            $domain ="https://";
        }
        elseif ( ! empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off')
        {
            $domain ="https://";
        }

        return $domain.$_SERVER['HTTP_HOST'];
    }

    /**
     * 用来加密验证
     * @return string
     */
    public function verification(){

        $key = $this->key;
        $verification = md5(date('YmdHi',time()).$key);

        return $verification;
    }

    /*
    * desc 获取session_id
    * 添加session有效期
    * */
    public function getSessionId(){

        $session_id = session_id();

        session('expire',time());

        return $session_id;

    }
    /*
     * 检查session
     * 2小时过期
     * */
    public function checkSession(){

        $expire = session('expire');

        if(is_null($expire)) return;

        $now_time=time()-$expire;

        //
        if($now_time>7200){

            session_destroy();

            $this->returnData(array(),'',400);


        }

        session('expire',time());

    }



}